import React from 'react';
import Store from 'store';
import { Redirect } from 'react-router-dom';
import Loadable from 'react-loadable';
import Config from '../common/config';
import { Spin, Alert } from 'antd';
import styles from '../index.less';
import Home from '../containers/Home';
import BasicLayout from '../layouts/BasicLayout';
import * as Route from "./index";
const routers = {
    'home': {
      label: 'home',
      icon: 'home',
      path: '/',
      exclude: true, //菜单显示
      exact: true,
      render: Home
    },
    ...Route
}

const deepRouter = (list) => {
  return list.map((vl,i)=> {
    if(vl.render === vl.children ) {
      const values = Object.values(vl);
      return deepRouter(values);      
    }
    return vl;
  })
}

export const getRouterMenu = () =>{ 
  let values = Object.values(routers);
  values = values.filter((vl)=> {
    return !vl.exclude;
  })
  const menuList = deepRouter(values);
//   console.info(menuList,"打印获取的菜单列表----")
  return menuList[0];
}

export const getLayoutData = layout => {
  return [{
      path: '/',
      exact: false,
      render: BasicLayout
    }
    
  ]
}

const deepCheckRoute = (list,fn) =>{ 
  list.forEach((vl,i)=> {
     if(!vl.render) {
      if(vl.children) {
        deepCheckRoute(vl.children, fn);
      } else {
        const values = Object.values(vl);
        deepCheckRoute(values, fn);
      }
    } else {
      fn(vl);
    }
  })
}

const getRouterTemplate = () => {
  const values = Object.values(routers);
  const routeObject = [];
  deepCheckRoute(values,(vl)=> {
    routeObject.push(vl);
  });
  return routeObject;
}

export { 
  getRouterTemplate,
  routers
};